package com.xizi.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xizi.order.mapper.RefundInfoMapper;
import com.xizi.order.service.RefundInfoService;
import com.xizi.yygh.enums.RefundStatusEnum;
import com.xizi.yygh.model.order.PaymentInfo;
import com.xizi.yygh.model.order.RefundInfo;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundInfoServiceImpl
        extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    //保存退款记录
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //根据订单id和支付类型
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",paymentInfo.getOrderId());
        wrapper.eq("payment_type",paymentInfo.getPaymentType());
        // 查询出退款信息
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);
        //若存在 直接返回
        if(refundInfo != null) {
            return refundInfo;
        }
        //否则 添加新的记录
        refundInfo = new RefundInfo();
        //创建时间
        refundInfo.setCreateTime(new Date());
        //订单编号
        refundInfo.setOrderId(paymentInfo.getOrderId());
        //支付类型
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        //对外业务编号
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        //退款状态
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        //交易内容
        refundInfo.setSubject(paymentInfo.getSubject());
        //退款金额
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        //插入数据
        baseMapper.insert(refundInfo);
        //再返回退款信息
        return refundInfo;
    }
}
